/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.component.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Interface;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.ElementFigure;
import com.cea.papyrus.diagram.composite.editpart.GraphNodeEditPolicy;
import com.cea.papyrus.diagram.composite.figure.ProvidedInterfaceFigure;
import com.cea.papyrus.diagram.composite.figure.RequiredInterfaceFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphConnector;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controler of  an {@link Interface} in the composite diagram
 * its representation is a circle or a half circle.
 */
public class InterfaceEditPart extends com.cea.papyrus.diagram.composite.editpart.InterfaceEditPart {

	/**
	 * Creates a new InterfaceEditPart.
	 * @param graphNode the graph node representing the interface
	 * @param modelManager the model manager associated to the editor
	 */
	public InterfaceEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * {@inheritDoc}
	 */
	protected void createEditPolicies(){
		super.createEditPolicies();
		// overload the GRAPHICAL_NODE_ROLE editPolicy to use the diagram specific one
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphNodeEditPolicy() );
	}

	/**
	 * {@inheritDoc}
	 */
	protected ElementFigure getNodeFigure(){
		return (ElementFigure)createFigure() ;
	}


	/**
	 * {@inheritDoc}
	 */
	protected IFigure createFigure() {

		if(((GraphNode)getGraphNode().getSemanticParent())!=null){

			Element elt = ((Uml1SemanticModelBridge)((GraphNode)getGraphNode().getSemanticParent()).getSemanticModel()).getElement();

			if (elt instanceof Component) {
				Component comp=(Component) elt;
				if (comp.getRequireds().contains(getUmlElement())){
					return new RequiredInterfaceFigure();
				}
				else{
					return new ProvidedInterfaceFigure();
				}
			} else
				// let super (from composite) do the job 
				return super.createFigure();
		}
		// else
			// semanticParent is null which should never happen
		return new ProvidedInterfaceFigure();
	}


	/**
	 * {@inheritDoc}
	 */
	protected void refreshVisuals() {

		/** Stereotype presentation */
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		if( fig instanceof RequiredInterfaceFigure){
			if(getGraphNode().getAnchorage().size()!=0){
				GraphConnector gc=(GraphConnector)getGraphNode().getAnchorage().get(0);
				Rectangle insideRect=r.getCopy().crop(new Insets(5));
				int gcPos=insideRect.getPosition(gc.getPosition());

				if( gcPos==PositionConstants.NORTH){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.SOUTH);}
				if( gcPos==PositionConstants.SOUTH){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.NORTH);}
				if( gcPos==PositionConstants.EAST){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.WEST);}					
				if( gcPos==PositionConstants.WEST){
					((RequiredInterfaceFigure)fig).setOrientation(PositionConstants.EAST);}
			}

		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				fig,
				r);
	}

}

